<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 5);

include_once("validate.php");
include_once("includes/style.php");
include_once("includes/event_details.php");
include_once("includes/addMessage.php");
include_once('includes/side_menu.php');


$style = new style(true);	

$done = false;
// are we showing an actual profile?
if(isset($_GET['id']))
{

        $userextra = "";
        if(isset($_SESSION['valid_id']) && is_numeric($_SESSION['valid_id']))
        {
            $userextra = ',(SELECT cs_driver FROM '. CHAMPSIGNUPS_TABLE . '
                      WHERE cs_champ = c_id AND cs_driver = ' . $_SESSION['valid_id'] . ') as champentered,
                     (SELECT es_driver FROM '. EVENTSIGNUPS_TABLE . '
                      WHERE es_event = e_id AND es_driver = ' . $_SESSION['valid_id'] . ') as evententered,
                     (SELECT l_order FROM ' . LICENSES_TABLE . ', ' . LCUSERS_TABLE . '
                      WHERE u_license = l_id AND u_id = ' . $_SESSION['valid_id'] .') as userlicense';

        }

	// Create query
	$q = "SELECT *, COUNT(*) as result_count " . $userextra . "
		  FROM " . CHAMPIONSHIPS_TABLE .  ", " . TRACKS_TABLE . ", " . GAMES_TABLE . ", ". LICENSES_TABLE . ", " . EVENTS_TABLE . "
                  LEFT JOIN " . RACERESULTS_TABLE . " ON (r_event = e_id)
		  WHERE c_license = l_id AND e_track = t_id AND e_championship = c_id AND c_enabled = 1 AND c_game = g_id AND
                        e_id = ". $_GET['id'] . "
                  GROUP BY e_id";
	// Run query
	$res = $db->sql_query($q);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$style->addRenderLeft(new side_menu('events', $_GET['id'], $row));
	
		if(isset($_GET['mode']))
		{
			$event = new event_details($row, $_GET['mode']);
		}
		else
		{
			$event = new event_details($row, 'overview');
		}
		$style->addRenderRight($event);
		$style->render();
		$done = true;
	}
}

if(!$done)
{
	include_once('includes/renderInclude.php');
	
	$style = new style(false);				
	
	if(isset($_GET['p']))
	{
		$page = $_GET['p'];
		$start =($page * 10) - 10;
	}
	else
	{
		$page = 1;
		$start = 0;
	}
	$text = $lang['EventsText'] . "<br><br>";

        $userextra = "";
        if(isset($_SESSION['valid_id']) && is_numeric($_SESSION['valid_id']))
        {
            $userextra = ',(SELECT cs_driver FROM '. CHAMPSIGNUPS_TABLE . '
                      WHERE cs_champ = c_id AND cs_driver = ' . $_SESSION['valid_id'] . ') as champentered,
                     (SELECT es_driver FROM '. EVENTSIGNUPS_TABLE . '
                      WHERE es_event = e_id AND es_driver = ' . $_SESSION['valid_id'] . ') as evententered,
                     (SELECT l_order FROM ' . LICENSES_TABLE . ', ' . LCUSERS_TABLE . '
                      WHERE u_license = l_id AND u_id = ' . $_SESSION['valid_id'] .') as userlicense';

        }

	
	if(isset($_GET['action']) && $_GET['action'] == "pastevents")
	{
		// Create query
		$q = "SELECT e_id, t_logo, t_fullTitle, c_title, c_id, c_logoThmbURL, e_title, e_openSignUps, t_location, e_description, l_imageThmb, l_order, g_logoThmb, count(*) as result_count,
					(SELECT e.ed_date FROM lc_eventdates e WHERE e.ed_event = e_id ORDER BY e.ed_date DESC LIMIT 0,1) as ed_date2 " . $userextra . "
					FROM " . EVENTDATES_TABLE . ", " . CHAMPIONSHIPS_TABLE .  ", " . GAMES_TABLE .  ", " . TRACKS_TABLE . ", " . LICENSES_TABLE . ", " . EVENTS_TABLE . "
                                        LEFT JOIN " . RACERESULTS_TABLE . " ON (r_event = e_id)
					WHERE g_id = c_game AND e_track = t_id AND ed_event = e_id AND e_championship = c_id AND c_license = l_id AND c_enabled = 1 AND ed_date < NOW()
                                        GROUP BY e_id
					ORDER BY ed_date2 DESC LIMIT " . $start . ", 10";
		$text .= $lang['ViewButton'] . ": <a href='events.php'>" . $lang['EventsFuture'] . "</a> | " . $lang['EventsRecent'] . '<br>' . $lang['Page'] . ': ';
		
		$title = $lang['EventsRecent'];
		$url_action = 'events.php?action=pastevents&p=';
		$qC = "SELECT COUNT(DISTINCT e_id)  as row_count
			   FROM " . EVENTDATES_TABLE . ", " . EVENTS_TABLE . ", " . CHAMPIONSHIPS_TABLE . "
			   WHERE ed_event = e_id AND e_championship = c_id AND c_enabled = 1 AND ed_date < NOW()";
	}
	else
	{
		// Create query
		$q = "SELECT e_id, t_logo, t_fullTitle, c_title, c_id, c_logoThmbURL, e_title, e_openSignUps, t_location, g_logoThmb,
                             e_description, l_imageThmb, l_order, count(*) as result_count,
                            (SELECT e.ed_date FROM lc_eventdates e WHERE e.ed_event = e_id ORDER BY e.ed_date ASC LIMIT 0,1) as ed_date2 " . $userextra . "
                      FROM " . EVENTDATES_TABLE . ", " . CHAMPIONSHIPS_TABLE . ", " . GAMES_TABLE . ", " . TRACKS_TABLE . ", " . LICENSES_TABLE . ", " . EVENTS_TABLE ."
                      LEFT JOIN " . RACERESULTS_TABLE . " ON (r_event = e_id)
                      WHERE g_id = c_game AND e_track = t_id AND ed_event = e_id AND e_championship = c_id AND c_enabled = 1 AND c_license = l_id AND ed_date > NOW()
                      GROUP BY e_id
                      ORDER BY ed_date2 ASC LIMIT " . $start . ", 10";

		$text .= $lang['ViewButton'] . ": " . $lang['EventsFuture'] . " | <a href='events.php?action=pastevents'>" . $lang['EventsRecent'] . "</a><br>" . $lang['Page'] . ': ';
		$title = $lang['EventsFuture'];
		$url_action = 'events.php?action=recentevents&p=';
		$qC = "SELECT COUNT(DISTINCT e_id) as row_count
			   FROM " . EVENTDATES_TABLE . ", " . EVENTS_TABLE . ", " . CHAMPIONSHIPS_TABLE . "
			   WHERE ed_event = e_id AND e_championship = c_id AND c_enabled = 1 AND ed_date > NOW()";
			   
	}

	$res = $db->sql_query($qC);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$row['row_count'] = ceil($row['row_count'] / 10);
		for($x = 1; $x <= $row['row_count']; $x++)
		{
			if($page == $x)
			{
				$text .= $x . " | ";
			}
			else
			{
				$text .= "<a href='" . $url_action . $x . "'>" . $x . "</a> | ";
			}
		}
		
		if($text != '')
		{
			$text = substr($text, 0 , -2);
		}
	}
		
	
	$style->addRenderRight(new addMessage($text,$title));
					 
	
	// Run query
	$res = $db->sql_query($q);
	$style->addRenderRight(new renderInclude('tpl/tableStart.tpl'));
	while ( $row = $db->sql_fetchrow($res)  )
	{
		$style->addRenderRight(new event_details($row, 'MiniSolo'));
	}
	$style->addRenderRight(new renderInclude('tpl/tableEnd.tpl'));
	
	$style->render();
}

echo $db->num_queries;

?>